package com.helloandroid

import android.content.SharedPreferences
import androidx.core.content.edit


/**
 * 添加配置, 需要修改4个地方
 * 1. 解析的位置
 * 2. 存储的位置
 * 3. 读取的位置
 * 4. 复制的位置
 */
data class ServerConfig(
        var ignoreVersion:String = "",
        var hbyMax1Day:Int = 10,
        var txVideo:Int = 1,
        var myPageNativeAds:Int = 1,
        var zzPageNativeAds:Int = 1,  // 控制是否显示
        var newUserRedVideo:Int = 0, // 新用户奖励
        var dashboardDailyRedPacketVideo:Int = 1, // 仪表盘每日红包. 1为开
        var giveupDoubleRewardDelay:Int = 3000, // 放弃双倍延迟时间,默认为3000 ms
        var dailySignVideo:Int = 1, //  签到看视频
        var tthbsRewardVideo:Int = 1, // 天天红包赛领奖的时候是否需要看视频
){
    companion object{
        val DefaultServerConfig = ServerConfig()

        fun loadFromPreference(pref:SharedPreferences):ServerConfig{
            val ignoreVersion = pref.getString("ignoreVersion","")!!
            val hbyMax1Day = pref.getInt("hbyMax1Day",1)
            val txVideo = pref.getInt("txVideo",1)
            val myPageNativeAds = pref.getInt("myPageNativeAds",1)
            val zzPageNativeAds = pref.getInt("zzPageNativeAds",1)
            val newUserRedVideo = pref.getInt("newUserRedVideo",0)
            val dashboardDailyRedPacketVideo = pref.getInt("dashboardDailyRedPacketVideo",1)
            val giveupDoubleRewardDelay = pref.getInt("giveupDoubleRewardDelay",3000)
            val dailySignVideo = pref.getInt("dailySignVideo",1)
            val tthbsRewardVideo = pref.getInt("tthbsRewardVideo",1)
            return ServerConfig(
                    ignoreVersion,
                    hbyMax1Day,
                    txVideo,
                    myPageNativeAds,
                    zzPageNativeAds,
                    newUserRedVideo,
                    dashboardDailyRedPacketVideo,
                    giveupDoubleRewardDelay,
                    dailySignVideo,
                    tthbsRewardVideo
            )
        }

        // 配置是否存在
        fun isConfigExist(pref: SharedPreferences):Boolean{
            return pref.getInt("servercfg",0) > 0
        }
    }

    fun copyFrom(other:ServerConfig){
        ignoreVersion = other.ignoreVersion
        hbyMax1Day = other.hbyMax1Day
        txVideo = other.txVideo
        myPageNativeAds = other.myPageNativeAds
        zzPageNativeAds = other.zzPageNativeAds
        newUserRedVideo = other.newUserRedVideo
        dashboardDailyRedPacketVideo = other.dashboardDailyRedPacketVideo
        giveupDoubleRewardDelay = other.giveupDoubleRewardDelay
        dailySignVideo = other.dailySignVideo
        tthbsRewardVideo = other.tthbsRewardVideo
    }

    fun saveConfig(pref:SharedPreferences){
        pref.edit(commit = true){
            putString("ignoreVersion",ignoreVersion)
            putInt("hbyMax1Day",hbyMax1Day)
            putInt("txVideo",txVideo)
            putInt("myPageNativeAds",myPageNativeAds)
            putInt("zzPageNativeAds",zzPageNativeAds)
            putInt("newUserRedVideo",newUserRedVideo)
            putInt("dashboardDailyRedPacketVideo",dashboardDailyRedPacketVideo)
            putInt("giveupDoubleRewardDelay",giveupDoubleRewardDelay)
            putInt("dailySignVideo",dailySignVideo)
            putInt("tthbsRewardVideo",tthbsRewardVideo)
            putInt("servercfg",1)
        }
    }

}